//
//  CicumlistViewController.m
//  CivilianTravel
//
//  Created by xalo on 16/7/14.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "CicumlistViewController.h"
#import "ListTableViewCell.h"
#import "Listmodel.h"


//5533
#define kUrl1 @"http://tubu.ibuzhai.com/rest/v2/travelog/"
#define kUrl2 @"?&api_version=1&app_version=4.4.2&device_type=2&travelog_id="

@interface CicumlistViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSMutableArray *datasourcearray;

@end

@implementation CicumlistViewController
-(NSMutableArray *)datasourcearray{
    if (!_datasourcearray) {
        _datasourcearray = [NSMutableArray array];
            }return _datasourcearray;
}
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight)];
        _tableview.estimatedRowHeight =850;
        _tableview.rowHeight = UITableViewAutomaticDimension+320;
        
    }return _tableview;
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
 
    [self.tableview reloadData];
    
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //隐藏tablebarcontroller
    self.tabBarController.tabBar.hidden = YES;
    
    [self request];
    NSLog(@"%@",self.ID);
    NSString  *k1 = [kUrl1 stringByAppendingString:self.ID];
    NSString  *k2 = [kUrl2 stringByAppendingString:self.ID];
    NSString *kUrl = [k1 stringByAppendingString:k2];
    NSLog(@"%@",kUrl);
    
    [self.tableview registerNib:[UINib nibWithNibName:@"ListTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
   
   
    
}
#pragma mark  --------uitableview delegate-------------
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell setcellwithmodle:self.datasourcearray[indexPath.row]];
    return cell;
}
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasourcearray.count;
}


//网址请求
-(void)request{
    NSString  *k1 = [kUrl1 stringByAppendingString:self.ID];
    NSString  *k2 = [kUrl2 stringByAppendingString:self.ID];
    NSString *kUrl = [k1 stringByAppendingString:k2];
  
    
    [RequestManager fetchDataWithUrl:kUrl parameter:@{} method:@"GET" compeleted:^(NSData *data) {
        NSDictionary *DIC = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//        NSLog(@"%@",DIC);
        for (NSDictionary *dict  in DIC[@"log"][@"posts"]) {
            Listmodel *modle = [[Listmodel alloc]init];
            [modle setValuesForKeysWithDictionary:dict];
//            [modle setValuesForKeysWithDictionary:dict[@"pictures"]];
            [self.datasourcearray addObject:modle];
            
        }
    
    } updateUI:^{
        _tableview .dataSource =self;
        _tableview.delegate = self;
        self.tableview.separatorStyle = UITableViewCellSelectionStyleNone;
 [self.view addSubview:self.tableview];
        [self.tableview reloadData];
    
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
